//
//  AppDelegate.swift
//  weibo27
//
//  Created by mac on 16/4/16.
//  Copyright © 2016年 it.cast. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //设置全局的样式颜色  UITabBar的样式
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        window = UIWindow(frame: UIScreen .mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultController
        
        window?.makeKeyAndVisible()
        
//        print(CZUserAccountViewModel.shareModel.account)
//        print(CZUserAccountViewModel.shareModel.accountPath)
        
        //接收通知，切换控制器
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification, object: nil, //接收一切的通知
            queue: nil) //如果为nil就是在主线程就行回调
            { [weak self](NSNotification) -> Void in
                
                let vc = NSNotification.object != nil ? CZWelcomeController() : CZTabBarController()
                
                self?.window?.rootViewController = vc
        }
        
        return true
    }
    
    //移除通知
    deinit{
        
    NSNotificationCenter.defaultCenter().removeObserver(self, name: WBSwitchRootViewControllerNotification, object: nil)
        
    }

}

// MARK: 界面切换的控制器
extension AppDelegate{
    
    private var defaultController:UIViewController{
        
        //登录以后看是否为新版本，如果是就显示，如果不是就显示欢迎界面
        if CZUserAccountViewModel.shareModel.userLogin{
            
            return isNewVersion() ? CZNewFeatureController() : CZWelcomeController()
        }
        
        //如果没有登录就显示主控制器
        return CZTabBarController()
    }
    
    // MARK: 判断是否为新版本
    private func isNewVersion()->Bool{
        
//        print(NSBundle.mainBundle().infoDictionary)
        
        //当前的版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = Double(currentVersion)!
        
        //保存到用户的偏好设置中的版本号
        let sandBoxVersionKey = "sandBoxVersionKey"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandBoxVersionKey)
        
        //将当前的版本号存到用户的偏好设置中
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: sandBoxVersionKey)
        
        return version>sandBoxVersion
        
    }
    
}












